package com.valuegroup.smarttodo.ui.todo;


import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;

import com.valuegroup.smarttodo.R;
import com.valuegroup.smarttodo.ui.todo.habit.HabitFragment;
import com.valuegroup.smarttodo.ui.todo.rethink.RethinkFragment;
import com.valuegroup.smarttodo.ui.todo.task.TaskFragment;
import com.valuegroup.smarttodo.plugins.indicator.Indicator;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;


/**
 * 待办-首页
 */
public class TodoFragment extends Fragment {
    private static final String TAG = TodoFragment.class.getSimpleName();

    private Unbinder unbinder;

    @BindView(R.id.viewPager)
    ViewPager viewPager;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.indicator)
    Indicator indicator;

    public TodoFragment() {
    }

    public static TodoFragment newInstance() {
        return new TodoFragment();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_todo, container, false);
        unbinder = ButterKnife.bind(this, view);


        // 初始化工具条
        AppCompatActivity aty = ((AppCompatActivity) getActivity());
        aty.setSupportActionBar(toolbar);
        // 隐藏Toolbar的标题栏
        aty.getSupportActionBar().setDisplayShowTitleEnabled(false);
        // 需要在fragment中配置actionbar可先的属性,如果不设置这个属性，fragment中的onCreateOptionsMenu()回调不会被执行！
        setHasOptionsMenu(true);

        // 初始化选项卡
        viewPager.setAdapter(new TodoPagerAdapter(getChildFragmentManager()));
        viewPager.addOnPageChangeListener(pageChangeListener);
        indicator.setViewPager(viewPager);

        return view;
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        //先clear()一下, 这样按钮就只有Fragment中设置的自己的了, 不会有Activity中的按钮.
        menu.clear();
        inflater.inflate(R.menu.menu_task, menu);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    private ViewPager.OnPageChangeListener pageChangeListener = new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            Log.i(TAG, "select page:" + position);
            switch (position) {
                case 0:
                    // fastAddButton.showToast();
                    break;
                case 1:
                    // fastAddButton.hide();
                    break;
                default:
                    // fastAddButton.hide();
                    break;
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    };


    public class TodoPagerAdapter extends FragmentPagerAdapter {
        public final String TAG = TodoPagerAdapter.class.getSimpleName();

        private List<Fragment> tabFragments = new ArrayList<>();

        public TodoPagerAdapter(FragmentManager fm) {
            super(fm);
            tabFragments.add(TaskFragment.newInstance());
            tabFragments.add(HabitFragment.newInstance());
            tabFragments.add(RethinkFragment.newInstance());
        }

        @Override
        public Fragment getItem(int position) {
            return tabFragments.get(position);
        }

        @Override
        public int getCount() {
            return tabFragments.size();
        }

    }
}
